package com.qf.exception;


import com.qf.dto.ApiResult;
import com.qf.utils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

//需求：用来改造原本的异常情况下返回
//被ControllerAdvice修饰的类可以重新返回结果
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    //注解是用来识别要识别的异常类
    @ExceptionHandler(value=ArithmeticException.class)
    public ApiResult arithmeticException(ArithmeticException e){
        e.printStackTrace();

        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = IndexOutOfBoundsException.class)
    public ApiResult indexOutOfBoundsException(IndexOutOfBoundsException e){
        e.printStackTrace();

        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = NullPointerException.class)
    public ApiResult nullPointerException(NullPointerException e){
        e.printStackTrace();

        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public ApiResult exception(Exception e){
        e.printStackTrace();

        return R.fail(e.getMessage());
    }

    @ExceptionHandler(value = GlobalException.class)
    public ApiResult globalException(GlobalException e){
        e.printStackTrace();
        return R.fail(e.getApiEnum());
    }
}
